﻿Public Class HydroGeoSit


    Dim db As New Connexion
    Dim action As String
    Dim Entrer As String
    Dim TextBoxID As String
    Dim Niveau As Integer = 1
    Dim LibelleNiv As String()
    Dim CodeNiv As String()
    Dim NomNiv As String()
    Dim IdNiv As String()
    Dim Nomtable As String()
    Dim NbrNiv As Integer = 0
    Dim idAct As String = ""
    Public databaseName As String
    Dim idAct1 As String = ""
    Dim NbrNiv1 As Integer = 0
    Dim Niveau1 As Integer = 1
    Dim LibelleNiv1 As String()
    Dim CodeNiv1 As String()
    Dim NomNiv1 As String()
    Dim IdNiv1 As String()
    Dim Nomtable1 As String()

    Private Sub HydroGeoSit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ComboBoxCodeTyp.Visible = False
        preNiv1()
        majTypRoch()
    End Sub

   
    Private Sub majTypRoch()
        Dim requeteX As String
        Dim niv As Integer = 1


        requeteX = "Select * from  [dbo].[HydroGeolTyp] WHERE " & Nomtable(3 + NbrNiv) & " = '" & Niveau & "'"

        While niv < Niveau
            requeteX = requeteX & " and " & Nomtable(2 + niv) & " = '" & CodeNiv(niv - 1) & "' "
            niv = niv + 1
        End While


        If Niveau = 1 Then
            Me.ButtonBack.Enabled = False
            Me.ButtonNext.Enabled = True
        ElseIf Niveau = NbrNiv Then
            Me.ButtonBack.Enabled = True
            Me.ButtonNext.Enabled = False
        Else
            Me.ButtonBack.Enabled = True
            Me.ButtonNext.Enabled = True
        End If

        db.myReq = requeteX
        db.Connecter(databaseName)

        Me.ComboBoxCodeTyp.Items.Clear()
        Me.ComboBoxNomType.Items.Clear()
        Me.ComboBoxNomType.Text = ""
        While db.rdr.Read()
            Me.ComboBoxNomType.Items.Add(db.rdr(NbrNiv + 4))
            Me.ComboBoxCodeTyp.Items.Add(db.rdr(0))
        End While
        db.Disconnecter()

        If ComboBoxCodeTyp.Items.Count < 0 Or Niveau = NbrNiv Then
            Me.ButtonNext.Enabled = False
        Else
            Me.ButtonNext.Enabled = True
        End If

    End Sub
    Private Sub ComboBoxCodeTyp_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomType.TextChanged

        'If Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text) > 0 Then
        Dim Pos As Int32 = Me.ComboBoxNomType.Text.Length
        Me.ComboBoxNomType.SelectedIndex = Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text)
        Me.ComboBoxNomType.SelectionStart = Pos
        Me.ComboBoxNomType.SelectionLength = Me.ComboBoxNomType.Text.Length - Pos
        Me.ComboBoxCodeTyp.SelectedIndex = Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text)
        'Else
        'Me.ComboBoxCodeTyp.Text = ""
        'End If
    End Sub

    Private Sub preNiv1()
        db.myReq = "SELECT COUNT(syscolumns.name) AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'HydroGeolTyp'"
        db.Connecter(databaseName)
        While db.rdr.Read()
            NbrNiv = db.rdr(0) - 6
        End While
        db.Disconnecter()
        LibelleNiv = New String(NbrNiv) {}
        CodeNiv = New String(NbrNiv) {}
        NomNiv = New String(NbrNiv) {}
        IdNiv = New String(NbrNiv) {}
        Nomtable = New String(NbrNiv + 6) {}
        Dim iter As Integer = 1
        While iter < NbrNiv
            db.myReq = "SELECT [Libelle_Niveau] FROM  [dbo].[Niveau] Where [Table_Niveau]='HydroGeolTyp' and [Niv_Niveau]='" & iter & "'"
            db.Connecter(databaseName)
            While db.rdr.Read()
                LibelleNiv(iter - 1) = db.rdr(0)
            End While
            db.Disconnecter()
            iter = iter + 1
        End While

        db.myReq = "SELECT syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'HydroGeolTyp'"
        db.Connecter(databaseName)
        iter = 0
        While db.rdr.Read()
            Nomtable(iter) = db.rdr(0)
            iter = iter + 1
        End While
        db.Disconnecter()
    End Sub

    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Try
            Dim idMax As Integer
            db.myReq = "INSERT INTO  [dbo].[HydroGeolSit]([ID_Sit],[ExplFrm_HydroGeolSit]) VALUES('" & db.idSite & "','" & Me.TextBoxDesc.Text & "')"
            MsgBox(db.myReq)
            db.Connecter(databaseName)
            db.Disconnecter()
            db.myReq = "SELECT Max([Id_HydroGeolSit]) FROM  [dbo].[HydroGeolSit]"
            db.Connecter(databaseName)
            While db.rdr.Read
                idMax = db.rdr(0)
            End While
            db.Disconnecter()
            db.myReq = "INSERT INTO  [dbo].[HydroGeolFrn2]([Id_HydroGeolTyp],[Id_HydroGeolSit],[Loc_HydroGeolFrn],[Expl_HydroGeolFrn]) VALUES('" & Me.ComboBoxCodeTyp.Text & "','" & idMax & "','" & Me.TextBoxLoc.Text & "','" & Me.TextBoxDesc.Text & "')"
            MsgBox(db.myReq)
            db.Connecter(databaseName)
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Dim typX As New TypologieNivXssCatg
        typX.Text = "Typologie"
        typX.Table2 = "HydroGeolTyp"
        typX.Visible = True

    End Sub

    Private Sub ButtonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext.Click
        Entrer = ""
        Niveau = Niveau + 1
        Me.LabelNiv.Text = LibelleNiv(Niveau)

        db.myReq = "Select * from  [dbo].[HydroGeolTyp] WHERE " & Nomtable(0) & " = '" & Me.ComboBoxCodeTyp.Text & "'"
        db.Connecter(databaseName)
        While db.rdr.Read
            CodeNiv(Niveau - 2) = db.rdr(1 + Niveau)
        End While

        majTypRoch()
    End Sub

    Private Sub ButtonBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBack.Click
        Dim niv As Integer = 0
        Me.TextBoxDecoupageHab.Text = ""
        Entrer = ""
        Niveau = Niveau - 1
        Me.LabelNiv.Text = LibelleNiv(Niveau - 1)
        While niv < Niveau
            Me.TextBoxDecoupageHab.Text = Me.TextBoxDecoupageHab.Text & NomNiv(niv)
            niv = niv + 1
        End While
        majTypRoch()
        ' Me.ComboBoxNomType.SelectedIndex = Me.ComboBoxNomType.Items.IndexOf(IdNiv(Niveau - 1))
    End Sub

    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        Me.TextBoxDesc.Text = ""
        Me.TextBoxLoc.Text = ""
        Me.TextBoxCarRoch.Text = ""
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    End Sub
End Class
